package LeetCode;

import java.util.HashSet;

public class LC_391_PerfectRectangle {

    public static void main(String[] args) {

    }

    public class Solution {
        public boolean isRectangleCover(int[][] rectangles) {
            int left = Integer.MAX_VALUE;
            int right = Integer.MIN_VALUE;
            int top = Integer.MIN_VALUE;
            int bottom = Integer.MAX_VALUE;
            HashSet<String> flags = new HashSet<>();
            int totalArea = 0;
            for (int[] rectangle : rectangles) {
                left = Math.min(left, rectangle[0]);
                bottom = Math.min(bottom, rectangle[1]);
                right = Math.max(right, rectangle[2]);
                top = Math.max(top, rectangle[3]);
                totalArea += (rectangle[3] - rectangle[1]) * (rectangle[2] - rectangle[0]);
                String pointLT = rectangle[0] + " " + rectangle[3];
                String pointLB = rectangle[0] + " " + rectangle[1];
                String pointRT = rectangle[2] + " " + rectangle[3];
                String pointRB = rectangle[2] + " " + rectangle[1];
                if (!flags.contains(pointLT)) flags.add(pointLT);
                else flags.remove(pointLT);
                if (!flags.contains(pointLB)) flags.add(pointLB);
                else flags.remove(pointLB);
                if (!flags.contains(pointRT)) flags.add(pointRT);
                else flags.remove(pointRT);
                if (!flags.contains(pointRB)) flags.add(pointRB);
                else flags.remove(pointRB);
            }
            return flags.size() == 4 && flags.contains(left + " " + top)
                    && flags.contains(left + " " + bottom)
                    && flags.contains(right + " " + bottom)
                    && flags.contains(right + " " + top)
                    && totalArea == (right - left) * (top - bottom);
        }

    }


    public class Solution_2 {
        public boolean isRectangleCover(int[][] rectangles) {
            int[] array = {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE};
            HashSet<String> vertices = new HashSet<>();
            long areaSum = 0;
            for (int[] rectangle : rectangles) {
                array[0] = Math.min(array[0], rectangle[0]);
                array[1] = Math.min(array[1], rectangle[1]);
                array[2] = Math.max(array[2], rectangle[2]);
                array[3] = Math.max(array[3], rectangle[3]);
                areaSum += calculateArea(rectangle);
                String[] fourVertices = getVertices(rectangle);
                for (String vertex : fourVertices) {
                    if (vertices.contains(vertex))
                        vertices.remove(vertex);
                    else
                        vertices.add(vertex);
                }
            }
            for (String vertex : getVertices(array)) {
                if (vertices.contains(vertex))
                    vertices.remove(vertex);
                else
                    return false;
            }
            return (vertices.isEmpty() && areaSum == calculateArea(array));
        }

        String[] getVertices(int[] rect) {
            String[] ans = new String[4];
            ans[0] = String.valueOf(rect[0]) + "," + String.valueOf(rect[1]);
            ans[1] = String.valueOf(rect[2]) + "," + String.valueOf(rect[1]);
            ans[2] = String.valueOf(rect[2]) + "," + String.valueOf(rect[3]);
            ans[3] = String.valueOf(rect[0]) + "," + String.valueOf(rect[3]);
            return ans;
        }

        long calculateArea(int[] rect) {
            long xSide = (long) rect[2] - (long) rect[0];
            long ySide = (long) rect[3] - (long) rect[1];
            return (xSide * ySide);
        }

    }
}